
;$(function(){
    // 显示form error
    var upload_pic_inputs = $(".upload_pic_input");
    for (var i =0; i < upload_pic_inputs.length; i ++) {
        var image_control_id = $(upload_pic_inputs[i]).attr('control_id');
        if ($("#" + image_control_id).siblings("label").length > 0){
            $("#" + image_control_id).parent().parent().show();
        }
    }

    $('div.close_icon_button').click(function(){
        if(confirm('你确定要修改或者删除吗？')){
            var control_id = $(this).attr("control_id");
            var image_id = $(this).attr("image_id");
            $(this).parent().addClass('hide');
            $(this).parent().siblings("img").addClass('hide').attr('src', '');
            $(this).parent().siblings("label").removeClass('hide');
            $('#' + control_id).removeAttr('value');
            $('#' + image_id).removeAttr('value');
        }else{
            return false;
        }
    });

    $('input.upload_pic_input').change(function(){
        var el = $(this);
        var form_data = new FormData();
        var logo = el.attr("logo");
        var file_obj = document.getElementById(el.attr("id")).files[0];
        form_data.append("file", file_obj);
        if (null != logo) {
            form_data.append("logo", logo);
        }
        var control_id = el.attr('control_id');
        var image_id = el.attr('image_id');
        $.ajax({
            type: "POST",
            url: d + "/quanquan-resource/image/upload",
            data: form_data,
            cache: false,
            contentType: false,
            processData: false,
            success: function(d) {
                let res = d.data;
                let src = res.imageAddress;
                let id = res.id;
                $('#' + control_id).val(src);
                if (image_id != null && image_id != undefined) {
                    $('#' + image_id).val(id);
                }
                if (control_id != null && control_id != undefined) {
                    $('#' + control_id).val(src);
                }
                console.log(image_id + ":" + $('#' + image_id).val());
                el.siblings('div.mark').removeClass('hide');
                el.siblings("img").attr("src", src).load(function() {
                    el.siblings("img").removeClass('hide')
                });
                el.siblings("label").addClass('hide');
            }
        });
    });
});

function sendFile(file, editor, sum_control) {
    var data = new FormData();
    data.append("file", file);
    $.ajax({
        data: data,
        type: "POST",
        url: d + "/quanquan-resource/image/upload",
        // url: "http://47.103.42.249:8040/quanquan-resource/image/upload",
        // url: "http://myzuu.com:8040/quanquan-resource/image/upload",
        cache: false,
        contentType: false,
        processData: false,
        success: function (data) {
            var url = data.src;
            sum_control.summernote('insertImage', url);
        }
    });
}
